#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void save_acc(void)
{
	char path[PATH_MAX];
	Account acc;
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("acc path:%s\n",path);
	int fd = open(path,O_RDWR);
	if(ERROR == fd)
	{
		perror("open acc");
		sprintf(stoc.str,"对不起，该用户不存在");
	}
	else
	{
		read(fd,&acc,sizeof(Account));	
		
		acc.money += ctos.acc.money;
		lseek(fd,SEEK_SET,0);
		write(fd,&acc,sizeof(Account));
		sprintf(stoc.str,"恭喜您存款成功,您的账户余额为:%f",acc.money);	
	}
	// 返回开户成功信息
	stoc_send(&stoc);
}

int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_SAVE);
		stoc.type = ctos.pid;
		save_acc();
	}
}